package com.zbkj.crmeb.merchant.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.crmeb.core.common.CommonResult;
import com.zbkj.crmeb.merchant.admin.service.MerAgentHomePageService;
import com.zbkj.crmeb.statistics.response.HomeRateResponse;
import com.zbkj.crmeb.statistics.service.HomeService;
import com.zbkj.crmeb.system.service.SystemAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 服务商-控制台
 *
 * @author lvmd
 * @date 2022-02-18
 */
@Slf4j
@RestController
@RequestMapping("api/admin/mer/agent/statistics")
@Api(tags = "服务商-控制台")
public class MerAgentHomePageController extends AdminBaseController {

    @Resource
    private MerAgentHomePageService agentHomePageService;
    @Resource
    private SystemAdminService systemAdminService;
    @Resource
    private HomeService homeService;

    /**
     * 销售额
     */
    @ApiOperation(value = "销售额")
    @GetMapping(value = "/sales")
    public CommonResult<HomeRateResponse> sales() {
        List<Integer> merIds = systemAdminService.getAdminAgentMerIds();
        if (CollectionUtils.isEmpty(merIds)) {
            return CommonResult.success(new HomeRateResponse(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO + "%", null, BigDecimal.ZERO));
        }
        return CommonResult.success(agentHomePageService.sales(merIds));
    }

    /**
     * 订单量
     */
    @ApiOperation(value = "订单量")
    @GetMapping(value = "/order")
    public CommonResult<HomeRateResponse> orders() {
        List<Integer> merIds = systemAdminService.getAdminAgentMerIds();
        if (CollectionUtils.isEmpty(merIds)) {
            return CommonResult.success(new HomeRateResponse(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO + "%", null, BigDecimal.ZERO));
        }
        return CommonResult.success(agentHomePageService.orders(merIds));
    }

    /**
     * 用户访问量
     */
    @ApiOperation(value = "用户访问量")
    @GetMapping(value = "/views")
    public CommonResult<HomeRateResponse> views() {
        List<Integer> merIds = systemAdminService.getAdminAgentMerIds();
        if (CollectionUtils.isEmpty(merIds)) {
            return CommonResult.success(new HomeRateResponse(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO + "%", null, BigDecimal.ZERO));
        }
        return CommonResult.success(homeService.views(merIds));
    }

    /**
     * 用户曲线图
     */
    @ApiOperation(value = "用户曲线图")
    @GetMapping(value = "/chart/user")
    public CommonResult<Map<Object, Object>> chartUser() {
        List<Integer> merIds = systemAdminService.getAdminAgentMerIds();
        if (CollectionUtils.isEmpty(merIds)) {
            return CommonResult.success(new HashMap<>());
        }
        return CommonResult.success(agentHomePageService.chartUser(merIds));
    }

    /**
     * 用户购买统计
     */
    @ApiOperation(value = "用户购买统计")
    @GetMapping(value = "/chart/user/buy")
    public CommonResult<Map<String, Integer>> chartUserBuy() {
        List<Integer> merIds = systemAdminService.getAdminAgentMerIds();
        if (CollectionUtils.isEmpty(merIds)) {
            return CommonResult.success(new HashMap<>());
        }
        return CommonResult.success(agentHomePageService.chartUserBuy(merIds));
    }

    /**
     * 订单量趋势
     */
    @ApiOperation(value = "30天订单量趋势")
    @GetMapping(value = "/chart/order/{type}")
    @ApiImplicitParam(name = "type", example = "lately30，week：本周，month：本月，year：年", value = "lately30:最近30天")
    public CommonResult<Map<String, Object>> chartOrder(@PathVariable String type) {
        if (StringUtils.isBlank(type)) {
            return CommonResult.failed();
        }
        List<Integer> merIds = systemAdminService.getAdminAgentMerIds();
        if (CollectionUtils.isEmpty(merIds)) {
            return CommonResult.success(new HashMap<>());
        }
        return CommonResult.success(agentHomePageService.chartOrder(merIds, type));

    }

    /**
     * 净利润
     */
    @ApiOperation(value = "净利润")
    @GetMapping(value = "/profit")
    public CommonResult<HomeRateResponse> profit() {
        List<Integer> merIds = systemAdminService.getAdminAgentMerIds();
        if (CollectionUtils.isEmpty(merIds)) {
            return CommonResult.success(new HomeRateResponse(BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO + "%", null, BigDecimal.ZERO));
        }
        return CommonResult.success(agentHomePageService.profit(merIds));
    }


}



